package parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;


import es.deusto.ingenieria.is.csp.formulation.Variable;

import properties.AdittionProblem;
import properties.Letter;


public class PuzzleParser extends XMLReader{

	public ArrayList<ArrayList<Letter>> getOperands() {
		return operands;
	}



	public void setOperands(ArrayList<ArrayList<Letter>> operands) {
		this.operands = operands;
	}



	public ArrayList<Letter> getResult() {
		return result;
	}



	public void setResult(ArrayList<Letter> result) {
		this.result = result;
	}



	public int getNumOperands() {
		return numOperands;
	}



	public void setNumOperands(int numOperands) {
		this.numOperands = numOperands;
	}



	public int getMaxLength() {
		return maxLength;
	}



	public void setMaxLength(int maxLength) {
		this.maxLength = maxLength;
	}



	public AdittionProblem getProblem() {
		return problem;
	}



	public void setProblem(AdittionProblem problem) {
		this.problem = problem;
	}



	private ArrayList<ArrayList<Letter>> operands;
	private ArrayList<Letter> result;
	private int numOperands;
	private int maxLength;
	private AdittionProblem problem;

	public PuzzleParser(String fileXML) {
		super(fileXML);
		// TODO Auto-generated constructor stub
	}

	

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		
		
		// TODO Auto-generated method stub
		try{
			if(qName.equals("is:puzzle")){
				operands  = new ArrayList<ArrayList<Letter>>();
				numOperands = Integer.parseInt(attributes.getValue("operands")); 
				maxLength = Integer.parseInt(attributes.getValue("operandMaxLength"));
				System.out.println("Number of operands ="+Integer.parseInt(attributes.getValue("operands")));
				System.out.println("Maximun length of the operands ="+Integer.parseInt(attributes.getValue("operandMaxLength")));
							
			}else{
				if(qName.equals("is:operand")){
					ArrayList<Letter> word = new ArrayList<Letter>();
					ArrayList<Integer> values = new ArrayList<Integer>();
					System.out.println("Word to add = "+attributes.getValue("word"));
					//we have to parse the giving word and add it to a list.
					String words = attributes.getValue("word");
					System.out.println(words);

					for(int j = 0 ; j<10;j++){
						values.add(j);
					}
					for(int i = 0;i<words.length();i++){
						String name = Character.toString(words.charAt(i));
						Letter letter = new Letter(name,values);
						word.add(letter);
					}
					operands.add(word);
				
				}else 
					if(qName.equals("is:result")){
						String words = attributes.getValue("word");
						System.out.println(words);
						result = new ArrayList<Letter>();
						ArrayList<Integer> values = new ArrayList<Integer>();
						for(int i = 0 ; i<10;i++){
							values.add(i);
						}
						for(int i = 0;i<words.length();i++){
							String name = Character.toString(words.charAt(i));
							Letter letter = new Letter(name,values);
							result.add(letter);
						}
					}
				}

		}catch(Exception exc){
			exc.printStackTrace();
		}
		
	}

}






	

